package com.xiaobai.frame.controller;


import com.xiaobai.common.utils.config.page.Query;
import com.xiaobai.common.utils.result.R;
import com.xiaobai.common.utils.result.Result;
import com.xiaobai.common.utils.result.ResultCode;
import com.xiaobai.common.utils.validate.EditGroup;
import com.xiaobai.frame.service.IDictService;
import com.xiaobai.model.entity.system.Dict;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-07
 */
@RestController
@RequestMapping("/dict")
@AllArgsConstructor
@Validated
@Tag(name = "字典表数据")
public class DictController {

    private final IDictService iDictService;

    @GetMapping("list")
    @Operation(description = "查询对应数据列表")
    public Result getList(@ParameterObject Query query,
                          @Parameter(description = "1查询全部 0查询具体字典参数") String type) {
        return R.success(iDictService.getList(query, type));
    }

    @PostMapping("add")
    @Operation(description = "新增数据")
    public Result addData(Dict dict) {
        return iDictService.addData(dict);
    }

    @PutMapping("update")
    @Operation(description = "修改数据")
    public Result updateData(@Validated(EditGroup.class) Dict dict) {
        if (iDictService.update(dict)) {
            return R.success(ResultCode.SUCCESS,"修改成功");
        }
        return R.fail(ResultCode.INTERNAL_SERVER_ERROR, "修改失败");
    }

}
